import {ADD_TODO, CLEAR_ALL_COMPLETED, DELETE_TODO, RECEIVE_TODOS, SELECT_ALL_TODOS} from './mutation-types'

/*接受组件通知，出发mutation调用，间接更新状态*/
export default {

  addTodo({commit},item){
    /*提交对mutation的请求*/
    commit(ADD_TODO,{item})
  },
  deleteTodo({commit},index){
    commit(DELETE_TODO,{index})
  },
  selectAllTodos({commit},isCheck){
    commit(SELECT_ALL_TODOS,{isCheck})
  },
  clearAllCompleted({commit}){
    commit(CLEAR_ALL_COMPLETED)
  },


  /*异步获取todos并更新状态*/
  reqTodos({commit}){
    /*模拟*/
    setTimeout(()=>{
      /*读取数据*/
      // const todos = ['你好','你好'];
      const todos = JSON.parse(window.localStorage.getItem('todos_key') || '[]');
      alert(todos);
      commit(RECEIVE_TODOS,{todos})
    },1000);
  },
}
